package com.hzcx.app.simplechat.ui.publicui;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseDialog;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.ui.publicui.adapter.PayListAdapter;
import com.hzcx.app.simplechat.ui.publicui.bean.HxUserInfoBean;
import com.hzcx.app.simplechat.ui.publicui.bean.PayListBean;
import com.hzcx.app.simplechat.ui.publicui.contract.PayListContract;
import com.hzcx.app.simplechat.ui.publicui.presenter.PayListPresenter;
import com.hzcx.app.simplechat.util.UserInfoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 支付弹窗界面
 */
public class PayListDialog extends BaseDialog<PayListPresenter> implements PayListContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.rv_pay)
    RecyclerView rvPay;
    @BindView(R.id.btn_pay)
    Button btnPay;

    private List<PayListBean> payData;
    private PayListAdapter payListAdapter;
    private HxUserInfoBean hxUserInfoBean;

    public PayListDialog(@NonNull Context context,HxUserInfoBean hxUserInfoBean) {
        super(context);
        this.hxUserInfoBean=hxUserInfoBean;
    }

    @Override
    public int getLayout() {
        return R.layout.dialog_pay_list;
    }

    @Override
    public void initPresenter() {
        mPresenter = new PayListPresenter();
    }

    @Override
    public void initView() {
        // 显示在最底部
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        ivClose.setOnClickListener(view -> dismiss());

        payData = new ArrayList<>();
        payListAdapter = new PayListAdapter(payData);
        rvPay.setLayoutManager(new GridLayoutManager(context, 3));
        rvPay.setAdapter(payListAdapter);

        if (UserInfoUtil.getUserIsVip()) {
            btnPay.setText("续费");
        }
    }

    @Override
    public void initData() {
        mPresenter.getPayList(context);

        payListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                payListAdapter.setPos(position);
            }
        });

        btnPay.setOnClickListener(view -> {
            ChatUtil.getInstance().toChatUi(context,hxUserInfoBean);
            dismiss();
        });
    }

    /**
     * 支付价格列表获取成功
     *
     * @param data
     */
    @Override
    public void payListResult(List<PayListBean> data) {
        try {
            payData.clear();
            payData.addAll(data);
            payListAdapter.notifyDataSetChanged();
            btnPay.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
